/**
 * 
 */
package net.ddns.heroli.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.gzheroli.bfw.pojo.base.TSUser;
import cn.com.gzheroli.cache.SpringRedisCache;
import cn.com.gzheroli.core.common.model.json.AjaxJson;
import cn.com.gzheroli.core.common.model.json.TreeGrid;
import cn.com.gzheroli.core.common.service.CommonService;
import cn.com.gzheroli.core.common.service.HeroService;
import cn.com.gzheroli.core.common.service.SystemService;
import cn.com.gzheroli.core.manager.Client;
import cn.com.gzheroli.core.util.NameHandler;
import cn.com.gzheroli.core.util.RequestUtil;
import cn.com.gzheroli.util.GenSqlUtil;
import net.ddns.heroli.pojo.TBGoodsType;
import net.ddns.heroli.pojo.TBSProType;

/**  
* 类名: GoodsController  
* 描述: 商户商品类别管理   
* 作者: 李长荣 lichang_20011@163.com  
* 日期: 2017年12月18日  下午12:47:09
*@version V1.0  
*    
*/
public class ShopTypeController {
	private static final Logger logger = LoggerFactory.getLogger(ShopTypeController.class);
	@Autowired
	private CommonService commonService;
	@Autowired
	private HeroService heroService;
	@Autowired
    private SpringRedisCache cache;
	@Autowired
	private SystemService systemService;
	@RequestMapping(value = "del")
	@ResponseBody
	public AjaxJson del(String id, HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("id不存在!");
			return j;
		}
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		TBSProType shopProType=commonService.get(TBSProType.class, id);
		shopProType.setUpdateBy(user.getId());
		shopProType.setUpdateTime(new Date());
		shopProType.setUpdateByName(user.getRealName());
		shopProType.setStatus("0");
		commonService.update(shopProType);
		j.setSuccess(true);
		j.setMsg("删除成功!");
		return j;
	}
	@RequestMapping(value = "save")
	@ResponseBody
	public  AjaxJson save(TBSProType shopProType, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		if(shopProType.getId()==null){
			shopProType.setUpdateBy(user.getId());
			shopProType.setUpdateTime(new Date());
			shopProType.setUpdateByName(user.getRealName());
			shopProType.setStatus("1");
			commonService.save(shopProType);
		}else{
			shopProType.setUpdateBy(user.getId());
			shopProType.setUpdateTime(new Date());
			shopProType.setUpdateByName(user.getRealName());
			commonService.update(shopProType);
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "update")
	@ResponseBody
	public  AjaxJson update( HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		String id=request.getParameter("id")==null?"":request.getParameter("id");
		TSUser user=client.getUser();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("保存失败!,编号不能为空");
			return j;
		}else{
			Map reqMap=RequestUtil.convertParameterToMap(request);
			reqMap.put("updateBy", user.getId());
			reqMap.put("updateTime", new Date());
			reqMap.put("updateByName", user.getRealName());
			heroService.updateMap("T_B_S_PRO_TYPE", reqMap, null, TBSProType.class);
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "list")
	@ResponseBody
	public  AjaxJson  list(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		//Client client=cache.getCache(request.getSession().getId(), Client.class);
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			//return null;
		}else{
			TSUser user=client.getUser();
		}
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		if(reqMap.containsKey("proId"))
		conMap.put("proId", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		Class clazz=TBSProType.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String sql=GenSqlUtil.genSqlByClass(clazz, conMap,"list",tableColumns,"am",reqMap);
		List beanList =commonService.findForJdbcPage(sql, clazz, reqMap, Integer.parseInt(page), Integer.parseInt(rows));
		j.setObj(beanList);
		String countSql=GenSqlUtil.genSqlByClass(clazz, conMap,"count",tableColumns,"am",reqMap);
		Long total=commonService.getCountForJdbc(countSql, reqMap);
		j.setTotal(total);
		return j;
	}
	@RequestMapping(value = "getShopProTypeTree")
	@ResponseBody
	public AjaxJson getShopProTypeTree(HttpServletRequest req,HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		String sql="select  * from T_B_S_PRO_TYPE where PARENT_ID is null and STATUS='1'";
		List<Map<String,Object>> treeMapList=new ArrayList<Map<String,Object>>();
		Map para=new HashMap();
		List<Map<String,Object>> treeList=commonService.findForJdbc(sql, para);
		for(Map<String,Object> t:treeList){
			Map<String,Object> m=new HashMap<String,Object>();
			m.put("label", t.get("name"));
			m.put("name", t.get("name"));
			m.put("id", t.get("id"));
			m.put("remark", t.get("remark"));
			m.put("type", "");
			List childList = new ArrayList();
			m.put("children", getChild(t.get("id").toString(),childList));
			treeMapList.add(m);
		}
		j.setObj(treeMapList);
		return j;
	}
	private List<Map<String,Object>> getChild(String id,List<Map<String,Object>> menuList){
		String sql="select  * from T_B_S_PRO_TYPE where PARENT_ID=#{parentId} and STATUS='1'";
		List<Map<String,Object>> treeMapList=new ArrayList<Map<String,Object>>();
		Map para=new HashMap();
		para.put("parentId", id);
		List<Map<String,Object>> treeList=commonService.findForJdbc(sql, para);
		for(Map<String,Object> t:treeList){
			Map<String,Object> m=new HashMap<String,Object>();
			m.put("label", t.get("name"));
			m.put("name", t.get("name"));
			m.put("id", t.get("id"));
			m.put("remark", t.get("remark"));
			m.put("type", "");
			m.put("parentId", id);
			List childList = new ArrayList();
			m.put("children", getChild(t.get("id").toString(),childList));
			treeMapList.add(m);
		}
		return treeMapList;
	}
}
